package com.lemon.smartsafety.utils;

import com.lemon.smartsafety.utils.common.LoggerHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 作者：machenike on 2016/4/11 10:35
 * 邮箱：
 * <p>
 * 作用:
 */
public class TimeUtil {


    /**
     * 获取当前日期
     *
     * @param i 为0时不截取日期位置
     * @return
     */
    public static String getNowTime(int i) {
        String strTime = "";

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        strTime = formatter.format(curDate);
        if (i == 0) {
            return strTime;
        } else {
            strTime = strTime.substring(0, i);
            return strTime;
        }

    }


    /**
     * 获取当前日期
     *
     * @param i 为0时不截取日期位置
     * @return
     */
    public static String getNowTimeSSS(int i) {
        String strTime = "";

        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        strTime = formatter.format(curDate);
        if (i == 0) {
            return strTime;
        } else {
            strTime = strTime.substring(0, i);
            return strTime;
        }

    }


    /**
     * 转换日期
     *
     * @param str
     * @return
     */
    public static Date tranTimeForDate(String str) {
        Date date = null;
        if (CommonUtil.strIsEmpty(str)) {
            return date;
        }


        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            date = formatter.parse(str);//获取当前时间
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return date;

    }


    /**
     * str1时间在str2试驾你之前返回 true
     *
     * @param str1
     * @param str2
     * @return
     */
    public static boolean dateCompareStr(String str1, String str2) {
        if (CommonUtil.strIsEmpty(str1) || CommonUtil.strIsEmpty(str2)) {
            return false;
        }

        try {
            Date date1, date2;
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            date1 = formatter.parse(str1);
            date2 = formatter.parse(str2);

            //如果a的时间在b之前（不包括等于）返回true
            if (date1.before(date2)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }


    /**
     * 转换日期
     *
     * @param str
     * @return
     */
    public static Date tranTimeForDateToDay(String str) {
        Date date = null;
        if (CommonUtil.strIsEmpty(str)) {
            return date;
        }


        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            date = formatter.parse(str);//获取当前时间
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return date;

    }


    public static Calendar getCalendarTime(Date date) {
        Calendar dateAndTime = Calendar.getInstance(Locale.CHINA);
        dateAndTime.setTime(date);

        return  dateAndTime;
    }


    /**
     * date1时间在date2时间之前返回 true
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean dateCompareDate(Date date1, Date date2) {
        if (CommonUtil.objIsEmpty(date1) || CommonUtil.objIsEmpty(date2)) {
            return false;
        }

        try {
            //如果a的时间在b之前（不包括等于）返回true
            if (date1.before(date2)) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


    }


    public static String transCalendarDate(int year, int monthOfYear, int dayOfMonth) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Calendar dateAndTime = Calendar.getInstance(Locale.CHINA);

        dateAndTime.set(Calendar.YEAR, year);
        dateAndTime.set(Calendar.MONTH, monthOfYear);
        dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);


        Date date = new Date();
        date = dateAndTime.getTime();

        String strTime = simpleDateFormat.format(date);
        LoggerHelper.error(TimeUtil.class, "您选择的日期和时间为:" + strTime);


        return strTime;
    }


    /**
     * 获取传入月份的当前月  所有天数
     *
     * @param date
     * @return
     */
    public static List<Date> getAllTheDateOftheMonthDate(Date date) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        List<Date> list = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);

        int month = cal.get(Calendar.MONTH);
        while (cal.get(Calendar.MONTH) == month) {
            list.add(cal.getTime());
            cal.add(Calendar.DATE, 1);
        }
        return list;
    }

    /**
     * 获取传入月份的当前月  所有天数
     *
     * @param date
     * @return
     */
    public static List<String> getAllTheDateOftheMonthStr(Date date) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        List<String> strlist = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        String strTime="";
        int month = cal.get(Calendar.MONTH);
        while (cal.get(Calendar.MONTH) == month) {

            strTime=cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1)+"-"+cal.get(Calendar.DAY_OF_MONTH);
            strlist.add(strTime);
            //strlist.add(formatter.format(cal.getTime()));
            cal.add(Calendar.DATE, 1);
        }
        return strlist;
    }

    /**
     * 转换日期
     *
     * @param str
     * @return
     */
    public static String transCalendarDatePicker(String str) {
        String strdate = null;
        if (CommonUtil.strIsEmpty(str)) {
            return strdate;
        }


        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = formatter.parse(str);//获取当前时间
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            strdate=cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1)+"-"+cal.get(Calendar.DAY_OF_MONTH);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return strdate;

    }


}
